#!/bin/bash
###############################################################################
#       脚本名称: test_ini.sh
#       功能描述: INI处理器(ini.sh)单元测试
###############################################################################

cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1

source ../test_framework.sh
source ../../../scripts/config/ini.sh

TEST_DIR=$(create_test_dir)

###############################################################################
# 测试用例
###############################################################################

test_ini_validate() {
    local test_file="$TEST_DIR/valid.ini"
    cat > "$test_file" << 'EOF'
[application]
name=MyApp
version=1.0.0

[database]
host=localhost
port=3306
EOF
    
    ini_validate "$test_file" 2>/dev/null
}

test_ini_read() {
    local test_file="$TEST_DIR/read_test.ini"
    cat > "$test_file" << 'EOF'
[application]
name=MyApp
version=2.0.0

[database]
host=localhost
port=3306
EOF
    
    local name=$(ini_read "$test_file" "application" "name" 2>/dev/null)
    assert_equals "MyApp" "$name"
    
    local port=$(ini_read "$test_file" "database" "port" 2>/dev/null)
    assert_equals "3306" "$port"
}

test_ini_write() {
    local test_file="$TEST_DIR/write_test.ini"
    
    ini_write "$test_file" "application" "name" "TestApp" 2>/dev/null
    ini_write "$test_file" "database" "port" "3306" 2>/dev/null
    
    local name=$(ini_read "$test_file" "application" "name" 2>/dev/null)
    assert_equals "TestApp" "$name"
    
    local port=$(ini_read "$test_file" "database" "port" 2>/dev/null)
    assert_equals "3306" "$port"
}

test_ini_delete() {
    local test_file="$TEST_DIR/delete_test.ini"
    cat > "$test_file" << 'EOF'
[application]
name=MyApp
temp=should_be_deleted
version=1.0.0
EOF
    
    ini_delete "$test_file" "application" "temp" 2>/dev/null
    
    local temp=$(ini_read "$test_file" "application" "temp" 2>/dev/null)
    assert_empty "$temp"
    
    local name=$(ini_read "$test_file" "application" "name" 2>/dev/null)
    assert_equals "MyApp" "$name"
}

test_ini_list_sections() {
    local test_file="$TEST_DIR/list_sections_test.ini"
    cat > "$test_file" << 'EOF'
[application]
name=MyApp

[database]
host=localhost

[server]
port=8080
EOF
    
    local sections=$(ini_list_sections "$test_file" 2>/dev/null)
    
    assert_contains "$sections" "application"
    assert_contains "$sections" "database"
    assert_contains "$sections" "server"
}

test_ini_list_keys() {
    local test_file="$TEST_DIR/list_keys_test.ini"
    cat > "$test_file" << 'EOF'
[database]
host=localhost
port=3306
username=root
EOF
    
    local keys=$(ini_list_keys "$test_file" "database" 2>/dev/null)
    
    assert_contains "$keys" "host"
    assert_contains "$keys" "port"
    assert_contains "$keys" "username"
}

test_ini_merge() {
    local target="$TEST_DIR/target.ini"
    local source="$TEST_DIR/source.ini"
    
    cat > "$target" << 'EOF'
[application]
name=TargetApp
version=1.0.0
EOF
    
    cat > "$source" << 'EOF'
[application]
name=SourceApp
port=8080
EOF
    
    ini_merge "$target" "$source" "merge" 2>/dev/null
    
    local name=$(ini_read "$target" "application" "name" 2>/dev/null)
    assert_equals "TargetApp" "$name"
    
    local port=$(ini_read "$target" "application" "port" 2>/dev/null)
    assert_equals "8080" "$port"
}

test_ini_format() {
    local test_file="$TEST_DIR/format_test.ini"
    cat > "$test_file" << 'EOF'
[application]
name   =   MyApp


version=1.0.0
EOF
    
    ini_format "$test_file" 2>/dev/null
    
    assert_file_exists "$test_file"
    ini_validate "$test_file" 2>/dev/null
}

test_ini_hash() {
    local test_file="$TEST_DIR/hash_test.ini"
    cat > "$test_file" << 'EOF'
[application]
name=MyApp
EOF
    
    local hash1=$(ini_get_hash "$test_file" 2>/dev/null)
    assert_not_empty "$hash1"
    
    ini_write "$test_file" "application" "version" "2.0.0" 2>/dev/null
    
    local hash2=$(ini_get_hash "$test_file" 2>/dev/null)
    assert_not_equals "$hash1" "$hash2"
}

###############################################################################
# 运行测试
###############################################################################

test_suite_start "INI处理器 (ini.sh) 单元测试"

run_test "INI文件验证" test_ini_validate
run_test "读取INI值" test_ini_read
run_test "写入INI值" test_ini_write
run_test "删除INI键" test_ini_delete
run_test "列出所有段落" test_ini_list_sections
run_test "列出段落的键" test_ini_list_keys
run_test "合并INI文件" test_ini_merge
run_test "格式化INI文件" test_ini_format
run_test "哈希计算" test_ini_hash

test_suite_end

cleanup_test_dir "$TEST_DIR"

test_report
exit $?

